namespace Plugins.Serializer.Internal
{
    using System;
    using System.Collections;
    using Plugins.Interfaces;

    [Serializable]
    internal class PluginFormatterCache
    {
        private readonly Hashtable hashtable = new Hashtable();

        public IPluginTypeFormatter this[IPluginTypeFormatter formatter]
        {
            get
            {
                IPluginRuntimeSerializerInternal serializer = PluginRuntimeHelper.GetSerializer(formatter);
                if (serializer != null)
                {
                    Type type = serializer.RuntimeType;
                    if (type != null)
                    {
                        if (!this.hashtable.ContainsKey(type))
                        {
                            this.hashtable[type] = formatter;
                        }
                    }
                    return (IPluginTypeFormatter) this.hashtable[type];
                }
                return null;
            }
        }

        public IPluginTypeFormatter this[Type type]
        {
            get
            {
                IPluginTypeFormatter formatter = null;
                if (type != null)
                {
                    if (!this.hashtable.ContainsKey(type))
                    {
                        formatter = new PluginTypeSerializer(type);
                        this.hashtable[type] = formatter;
                        return formatter;
                    }
                    return (IPluginTypeFormatter)this.hashtable[type];
                }
                return null;
            }
        }
    }
}

